<template>
  <div class='container'>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div class="text-search">
        <!-- 首行搜索行 -->
        <el-form label-width="80px" :model="page">
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="学科名称">
                <el-input class="text-box" v-model="page.subjectName"></el-input>
                <!-- 左边按钮 -->
                <el-button size="mini" @click="cleartext">清除</el-button>
                <el-button size="mini" type="primary" @click="getsubjectsList()">搜索</el-button>
              </el-form-item>
            </el-col>
            <!-- 右边新增学科按钮 -->
            <el-row type="flex" justify="end">
              <el-form-item>
                <el-button size="mini" type="success" icon="el-icon-edit" @click="addSubjectdialog = true">新增学科
                </el-button>
              </el-form-item>
            </el-row>
          </el-row>
        </el-form>
        <!-- 警告提示的消息数据行 -->
          <el-alert
            :title="`数据一共${total}条`"
            type="info"
            show-icon
            :closable="false">
          </el-alert>
          <!-- 列表区域 -->
          <el-table :data="SubjectsList" style="width: 100%">
            <el-table-column type="index" label="序号" width="100px"> </el-table-column>
            <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
            <el-table-column prop="username" label="创建者" width="100px"> </el-table-column>
            <el-table-column prop="addDate" label="创建日期" width="160px">
              <!-- 利用过滤器将时间格式化 (parseTimeByString方法路径在src/filters下)-->
              <template v-slot="{ row }">{{ row.addDate | parseTimeByString }}</template>              
            </el-table-column>
            <el-table-column prop="isFrontDisplay" label="前台是否显示" width="160px">
              <template v-slot="{ row }">
                {{ row.isFrontDisplay === 1 ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column prop="twoLevelDirectory" label="二级目录" width="160px"></el-table-column>
            <el-table-column prop="tags" label="标签" width="160px"></el-table-column>
            <el-table-column prop="totals" label="目标数量" width="100px"></el-table-column>
            <el-table-column label="操作" sortable="" fixed="right" width="280px">
              <template v-slot="{ row }">
                <el-button type="text" size="small" @click="subClass(row.id, row.subjectName)">学科分类</el-button>
                <el-button type="text" size="small" @click="subTags(row.id, row.subjectName)">学科标签</el-button>
                <el-button type="text" size="small" @click="EditSubject(row)">修改</el-button>
                <el-button type="text" size="small" @click="deleteSubject(row.id)">删除</el-button>
              </template>
            </el-table-column>        
          </el-table>
          <!-- 分页区域 -->
          <el-row type="flex" justify="end" align="middle" style="height: 60px">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="this.page.page"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="this.page.pagesize"            
              :total="total"
              background
              layout="prev, pager, next, sizes, jumper"
            >
            </el-pagination>
          </el-row>
          <!-- 新增学科的组件 -->
          <subjects-add :addSubjectdialog.sync="addSubjectdialog" @AddSubject="AddSubject" />
          <!-- 修改编辑学科 -->
          <el-dialog title="修改学科" :visible.sync="EditSubjectdialog" width="30%">
            <!-- 主体内容区域 -->
            <el-form label-width="100px">
              <el-form-item label="学科名称" prop="subjectName">
                <el-input placeholder="请输入学科名称" v-model="name"></el-input>
              </el-form-item>
              <!-- 是否显示 -->
              <el-form-item label="是否显示">
                <el-switch
                  v-model="value"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                >
                </el-switch>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="EditSubjectdialog = false">取 消</el-button>
              <el-button type="primary" @click="EditSubjectclass">确 定</el-button>
            </span>
          </el-dialog>          
      </div>
    </el-card>
  </div>
</template>

<script>
import { list, remove, detail, update } from '@/api/hmmm/subjects'
import { parseTimeByString } from '@/filters'
import  SubjectsAdd  from '@/module-hmmm/components/subjects-add'
export default {
  components: {
    SubjectsAdd
  },
  computed: {
    isSubjectNumber() {
      if (this.value) {
        return 1
      } else {
        return 0
      }
    }
  },
  data() {
    return {
      // 修改
      name: '',
      id: '',
      // 参数
      page: {
        page: 1,
        pagesize: 10,
        subjectName: '' //学科名称
      },
      total: 0,
      SubjectsList: [], // 学科列表
      addSubjectdialog: false, // 关闭新增学科的弹层
      EditSubjectdialog: false, // 控制修改学科对话框的显示与隐藏
      value: true
    }
  },
  created() {
    this.getsubjectsList()
  },
  methods: {
    // 获取学科列表数据
    async getsubjectsList() {
      const { data } = await list(this.page)
      // console.log(data)
      this.SubjectsList = data.items
      this.total = data.counts
    },
    // 页面尺寸大小(每页显示多少条)
    handleSizeChange(newsize) {
      this.page.pagesize = newsize
      this.getsubjectsList()
    },
    // 页面容量(当前页是多少条)
    handleCurrentChange(newpage) {
      this.page.page = newpage // 赋值最新的页码
      this.getsubjectsList() // 重新拉取数据
    },
    // 清除搜索框
    cleartext() {
      this.page.subjectName = ''
      this.getsubjectsList()
    },
    // 添加学科
    AddSubject() {
      this.addSubjectdialog = false
      this.getsubjectsList()
    },
    // 删除按钮
    async deleteSubject(id) {
      try {
        // console.log(id)
        await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示',
          { confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
        await remove({ id })
        // 删除成功，重新获取页面数据
        await this.getsubjectsList()
        this.$message({ type: 'success', message: '删除成功!' })
      } catch (error) {
        console.log(error)
      }
    },
    // 修改
    // 在开启对话框之前，获取到对应的数据
    async EditSubject(data1) {
      this.name = data1.subjectName
      // if (data1.isFrontDisplay === 0) {
      //   this.value = false
      // } else {
      //   this.value = true
      // }
      data1.isFrontDisplay === 0 ? false : true
      const { data } = await detail({ id: data1.id })
      console.log(data)
      this.id = data.id
      // 开启修改对话框
      this.EditSubjectdialog = true
    },
    // 点击修改按钮的确定按钮的时候，发起修改数据的请求
    async EditSubjectclass() {
      // console.log(id)
      const { data } = await update({
        id: this.id,
        subjectName: this.name,
        isFrontDisplay: this.isSubjectNumber
      })
      this.EditSubjectdialog = false
      this.getsubjectsList()
    },
    // 学科分类
    subClass(id, name) {
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    // 学科标签
    subTags(id, name) {
      this.$router.push({ path: 'tags', query: { id, name } })
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  .box-card {
    margin: 10px;
  }
  .text-box {
    width: 200px;
    padding-right: 15px;
  }
}
</style>
